<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/31
 * Time: 10:15
 */
namespace App\admin\Controllers;

use App\Facades\AdminC;
use App\Http\Controllers\Controller;
use App\Models\InviteLog;
use App\Models\Member;
use App\Models\TiCashLog;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Table;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use App\Models\TaskConfigLog;
use App\Models\LoginLog;
use App\Models\TaskConfig;

class NewUsersTrendController extends Controller
{
    use ModelForm;

    //新增用户走势
    public function newUsers()
    {
        $input = Input::all();
        $date = isset($input['date']) ? $input['date']: date('Y-m-d');
        $time_arr = $date;
        $end_date = date('Y-m-d',strtotime($date)+ 86400);
        $start_date = date('Y-m-d',strtotime($date) - 25924000);
        $sql = DB::select("select date(created_at) as date, count(*) AS nums FROM member where created_at between \"$start_date\" AND \"$end_date\" GROUP BY DATE(created_at)");
        $arr = [];
        for($i=0;$i<30;$i++) {
            array_unshift($arr, $time_arr);
            $time_arr = date('Y-m-d',strtotime($time_arr)-86400);
            $y_axis[$i] = 0;
        }
        $array = array_map('get_object_vars', $sql);
        foreach($array as $k => $v){
            if(in_array($v['date'], $arr)){
                $trans = array_flip($arr);
                $y_axis[$trans[$v['date']]] = $v['nums'];
            }
        }
        $x_axis = $arr;
        $x_axis = implode(',',$x_axis);
        $y_axis = implode(',',$y_axis);
        return view('admin.charts.bar', [
            'x_axis'=>$x_axis,
            'y_axis'=>$y_axis,
            'y_axis2'=>0,
            'name'=>'新增用户走势',
            'name2'=>0,
            'title'=>'新增用户走势'
        ]);
    }

    //活跃用户走势
    public function activeUsers()
    {
        $input = Input::all();
        $date = isset($input['date']) ? $input['date']: date('Y-m-d');
        $time_arr = $date;
        $end_date = date('Y-m-d',strtotime($date)+ 86400);
        $start_date = date('Y-m-d',strtotime($date) - 25924000);
        $arr = [];
        for($i=0;$i<30;$i++) {
            array_unshift($arr, $time_arr);
            $time_arr = date('Y-m-d',strtotime($time_arr)-86400);
            $y_axis[$i] = 0;
            $y_axis2[$i] = 0;
        }
        $x_axis = $arr;
        $x_axis = implode(',',$x_axis);
        $res = DB::select("select date(created_at) as date, count(DISTINCT member_id) AS nums FROM capital_log where created_at between \"$start_date\" AND \"$end_date\" GROUP BY DATE(created_at)");
        $array = array_map('get_object_vars', $res);
        foreach($array as $k => $v){
            if(in_array($v['date'], $arr)) {
                $trans = array_flip($arr);
                $y_axis[$trans[$v['date']]] = $v['nums'];
            }
        }
        $y_axis = implode(',',$y_axis);

        $active = LoginLog::selectRaw('date, count(*) AS nums')
            ->whereBetween('date',[$start_date,$end_date])
            ->groupBy('date')
            ->orderBy('date')
            ->get();
        foreach($active as $k => $v){
            if(in_array($v['date'], $arr)) {
                $trans = array_flip($arr);
                $y_axis2[$trans[$v['date']]] = $v['nums'];
            }
        }
        $y_axis2 = implode(',',$y_axis2);
        return view('admin.charts.bar', [
            'x_axis'=>$x_axis,
            'y_axis'=>$y_axis,
            'y_axis2'=>$y_axis2,
            'name'=>'收益用户走势',
            'name2'=>'访问用户走势',
            'title'=>'活跃用户走势'
        ]);
    }

    //邀请排行榜
     public function inviteRank(){
         $a = 1;
         $sql = Db::select("select invite_id, count(*) as nums from invite_log  GROUP BY invite_id ORDER BY nums desc");
         $invite_ids = [];
         foreach ($sql as $k=>$v){
             $invite_ids[] = $v->invite_id;
             $v->rank = $a;
             $a++;
         }
         $member_list = Member::select("avatar","name",'id')->whereIn('id', $invite_ids)->get();
         foreach ($sql as $k=>$v) {
             foreach ($member_list as $kk=>$vv) {
                if($v->invite_id === $vv->id){
                    $v->name = $vv->name;
                     $v->avatar = $vv->avatar;
                }
             }
         }

         return view('admin/invite_rank' , ["volist" => $sql]);
     }

    public function opreat()
    {
        return AdminC::content(function(Content $content){
            $content->header('邀请排行榜');
            $content->description('徒弟列表');
            $content->body(AdminC::grid(Member::class, function(Grid $grid) {
                $input = Input::all();
                $grid->model()->where('pid',$input['invite_id']);
                $grid->model()->select("id","name","avatar","sex","created_at","account");
                $grid->model()->orderBy("created_at","desc");
                $grid->id("Id");
                $grid->name("用户名");
                $grid->avatar("头像")->display(function ($avatar){
                    return '<img src="'.$avatar.'" width="120" height="80"/>';
                });
                $grid->sex("性别")->display(function($sex){
                    return $this->sex_arr[$sex];
                });
                $grid->created_at("创建时间");
                $grid->account("账户余额");
                $grid->disableExport();
                $grid->disableCreateButton();
                $grid->disableActions();
                $grid->disableFilter();
                $grid->disableRowSelector();
            }));
        });
    }
    public function tiCashLog(){
        //提现申请表格
        $input = Input::all();
        $date = isset($input['date']) ? $input['date']: date('Y-m-d');
        $time_arr = $date;
        $end_date = date('Y-m-d',strtotime($date)+ 86400);
        $start_date = date('Y-m-d',strtotime($date) - 25924000);
        $sql = DB::select("select date(created_at) as date, sum(money) AS nums FROM ticash where created_at between \"$start_date\" AND \"$end_date\" AND type = 0 GROUP BY DATE(created_at)");
        $arr = [];
        for($i=0;$i<30;$i++) {
            array_unshift($arr, $time_arr);
            $time_arr = date('Y-m-d',strtotime($time_arr)-86400);
            $y_axis[$i] = 0;
            $y_axis2[$i] = 0;
        }
        $array = array_map('get_object_vars', $sql);
        foreach($array as $k => $v){
            if(in_array($v['date'], $arr)){
                $trans = array_flip($arr);
                $y_axis[$trans[$v['date']]] = $v['nums'];
            }
        }
        $x_axis = $arr;
        $x_axis = implode(',',$x_axis);
        $y_axis = implode(',',$y_axis);
        $active = DB::select("select date(created_at) as date, sum(money) AS nums FROM ticash where created_at between \"$start_date\" AND \"$end_date\" AND type = 1 GROUP BY DATE(created_at)");
        $active = array_map('get_object_vars', $active);
        foreach($active as $k => $v){
            if(in_array($v['date'], $arr)) {
                $trans = array_flip($arr);
                $y_axis2[$trans[$v['date']]] = $v['nums'];
            }
        }
        $y_axis2 = implode(',',$y_axis2);
        //提现申请表格
        $list = TiCashLog::select()->paginate(20);
        return view('admin.charts.ticash', [
            'x_axis'=>$x_axis,
            'y_axis'=>$y_axis2,
            'y_axis2'=>$y_axis,
            'name2'=>'金币提现申请',
            'name'=>'现金提现申请',
            'title'=>'提现申请详情',
            'volist' => $list
        ]);
    }
}